package com.ssh.admin.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Brand;
import com.ssh.entity.Task;
import com.ssh.entity.TaskBrand;
import com.ssh.manager.TaskBrandManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/task/taskBrand.jsp"),
@Result(name="findList", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.task\\.content,page\\.pageData\\[\\d+\\]\\.brand\\.name,page\\.pageData\\[\\d+\\]\\.createDate," +
"page\\.pageData\\[\\d+\\]\\.startDate,page\\.pageData\\[\\d+\\]\\.endDate"}),
@Result(name="update", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="delete", type="json", params={"root","page","excludeproperties","page\\.curPageNum"})
})
public class TaskBrandAction extends ActionSupport{
	@Resource(name="taskBrandManagerImpl")
	private TaskBrandManager taskBrandManager;
	
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private TaskBrand taskBrand;
	private Brand brand;
	private Task task;
	private String ids;
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public JsonResult getJr() {
		return jr;
	}
	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public String getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}
	public TaskBrand getTaskBrand() {
		return taskBrand;
	}
	public void setTaskBrand(TaskBrand taskBrand) {
		this.taskBrand = taskBrand;
	}
	public Brand getBrand() {
		return brand;
	}
	public void setBrand(Brand brand) {
		this.brand = brand;
	}
	public String goJsp(){
		return "success";
	}
	
	public String add(){
		
		return "success";
	}
	
	public String findList(){
		Map conditionMap = new HashMap();
		conditionMap.put("task", task.getContent());
		conditionMap.put("brand", brand.getId());
		taskBrandManager.findList(conditionMap, page, isDesc);
		return "findList";
	}
	
	public String update(){
		
		return "update";
	}
	
	public String delete(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					taskBrandManager.delete(new Long(d[i]));
				}
			}else{
				taskBrandManager.delete(taskBrand.getId());
			}
			int _p = (int) page.getCurPageNum();
			page.setCurPageNum(1);
			Map conditionMap = new HashMap();
			conditionMap.put("task", task.getContent());
			conditionMap.put("brand", brand.getId());
			taskBrandManager.findList(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
		}
		return "delete";
	}
}
